|
|
|
|
|
|
|
Once you start creating class modules, youll begin to use these classes over and over again in your Visual Basic applications. For those classes that are used several times in one application, it can be tricky keeping track of all their instances. One mechanism that can simplify this process is the Collection class. |
|
|
|
|
|
|
|
|
The Collection class is a generic class you can use to hold a collection of your own objects. The Collection class allows you to add members to its internal array. It also allows you to retrieve those members later. Whats more, you can delete members from this collection and you dont have to worry about moving the other members around to fill in any holes in the collection. The Collection object will automatically reshuffle everything as appropriate to make sure there are no empty entries in the collection. |
|
|
|
|
|
|
|
|
With all of this shuffling around of members, it becomes pretty clear that you cant really use an index number to get to an element. As a result, the Collection class implements a property called Key thats assigned to each element as you add objects to the collection. The Key property is what you use to retrieve an object. The Key property must be unique among all of the members you add to this collection. |
|
|
|
|
|
|
|
|
If you want to access elements with an index number, you can. Just make sure you dont store the index number in a global variable because things move around in the collection and this index number might change. |
|
|
|
|
|
|
|
|
The Collection class only has one property, which is described in Table 22.1. |
|
|
|
|
| Table 22.1 Property of the Collection Class. | | Property | Description | | Count | Returns the total number of items in the collection. |
|
|
|
|
|
|
The Collection class has three methods, as shown in Table 22.2, you can use to add, retrieve, and delete members from the collection. |
|
|
|
|
|